package net.lab1024.sa.admin.module.business.ball.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.module.business.ball.bo.FuliBall;
import net.lab1024.sa.admin.module.business.ball.bo.Lottery3D;
import net.lab1024.sa.admin.module.business.ball.bo.Lotterykl8;
import net.lab1024.sa.admin.module.business.ball.service.BallService;
import net.lab1024.sa.admin.module.business.ball.service.Lottery3dService;
import net.lab1024.sa.admin.module.business.ball.service.LotteryKl8Service;
import net.lab1024.sa.admin.module.business.ball.vo.DoubleColorBallVo;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/ballController")
@RequiredArgsConstructor
@Slf4j
public class BallController {

    private final BallService ballService;
    private final Lottery3dService lottery3dService;
    private final LotteryKl8Service lotteryKl8Service;

    private final Integer defaultBatch = 20;

    private final Integer defaultLimit = 5;
    /**
     * 获取进5期的彩票数据
     */
    @GetMapping("/doubleColorBall")
    public ResponseDTO doubleColorBall() {
        LambdaQueryWrapper<FuliBall> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(FuliBall::getPhase).last("limit " + defaultLimit);
        List<FuliBall> list = ballService.list(queryWrapper);
        return ResponseDTO.ok(ballService.format(list));
    }
    @GetMapping("/happy8")
    public ResponseDTO happy8() {
        LambdaQueryWrapper<Lotterykl8> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Lotterykl8::getPhase).last("limit 5");
        return ResponseDTO.ok(lotteryKl8Service.list(queryWrapper));
    }
    @GetMapping("/fc3d")
    public ResponseDTO fc3d() {
        LambdaQueryWrapper<Lottery3D> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Lottery3D::getPhase).last("limit 5");
        return ResponseDTO.ok(lottery3dService.list(queryWrapper));
    }
    /**
     * 双色球过滤*期，红蓝色出现最小次数统计
     * flag:1 最多次数；2：最少次数
     * @param limit
     * @param flag
     * @return
     */
    @RequestMapping("/forecast")
    public ResponseDTO forecast(Integer limit, String flag) {
        flag = StringUtils.isBlank(flag) ? "2" : flag;
        limit = Optional.ofNullable(limit).orElse(defaultBatch);
         ResponseDTO res = ResponseDTO.ok(ballService.dbColorForecast(limit, flag));
//        res.setMsg("福彩双色球获取过去" + limit + "期内各个位置上出现次数" + ("1".equalsIgnoreCase(flag) ? "最多次数" : "最少次数") + "的数字");
        return res;
    }

    /**
     * 福彩3d获取过去"+limit+"期内各个位置上出现次数最少的数字
     * @param limit
     * @return
     */
    @RequestMapping("/forecast3d")
    public ResponseDTO forecast3d(Integer limit)  {
        limit = Optional.ofNullable(limit).orElse(defaultBatch);
        ResponseDTO res = ResponseDTO.ok(lottery3dService.threeDForecast(limit));
        return res;
    }

    /**
     * 福彩快乐8获取过去" + limit + "期内出现次数最少的数字,从小到大排序
     * @param limit
     * @return
     */
    @RequestMapping("/forecastkl8")
    public ResponseDTO forecastkl8(Integer limit)  {
        limit = Optional.ofNullable(limit).orElse(defaultBatch);
        ResponseDTO res = ResponseDTO.ok(lotteryKl8Service.kl8Forecast(limit));
        return res;
    }
    @RequestMapping("/kl8")
    public ResponseDTO kl8(Integer limit)  {
        lottery3dService.getAll3dData();
        lotteryKl8Service.getAllData();
        return ResponseDTO.ok();
    }

}

